package com.axend.aerosense.home;


import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.axend.aerosense.base.activity.MvvmBaseActivity;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.common.bean.AlarmBean;
import com.axend.aerosense.common.bean.EarlyWarningBean;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.home.fragment.HomeAlarmDetailsFragment;
import com.axend.aerosense.home.fragment.ReportLogDetailsFragment;
import com.axend.aerosense.home.R;
import com.axend.aerosense.home.databinding.HomeActivityLogDetailBinding;

@Route(path = RouterActivityPath.Home.PAGE_HOME_LOG_DETAIL)
public class LogDetailActivity extends MvvmBaseActivity<HomeActivityLogDetailBinding, BaseViewModel>
        implements ITitle {
    private EarlyWarningBean earlyWarningBean;
    private AlarmBean alarmBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        earlyWarningBean = (EarlyWarningBean) getIntent().getSerializableExtra(Gobal.IntentParamType.EARLY_OBJECT);
        alarmBean = (AlarmBean) getIntent().getSerializableExtra(Gobal.IntentParamType.WARN_OBJECT);

        if (null == earlyWarningBean && null == alarmBean) {
            finish();
        }

        ImmersionBar.with(this)
                .statusBarColor(android.R.color.white)
                .navigationBarColor(R.color.black)
                .autoDarkModeEnable(true)
                .fitsSystemWindows(true).init();
//        //刘海屏的刘海高度
//        int statusBarHeight = getStatusBarHeight();
//        viewDataBinding.homeActivityHome.setPadding(0,statusBarHeight,0,0);
        setSupportActionBar(viewDataBinding.homeToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        init();
    }

    @Override
    public void setTitle(String title) {
        viewDataBinding.homeTitle.setText(title);
    }


    private void init() {
        if (earlyWarningBean == null) {
            HomeAlarmDetailsFragment homeAlarmDetailsFragment = HomeAlarmDetailsFragment.getInstance(alarmBean);
            changeFragment(homeAlarmDetailsFragment);

        } else {
            ReportLogDetailsFragment homeAlarmDetailsFragment = ReportLogDetailsFragment.getInstance(earlyWarningBean);
            changeFragment(homeAlarmDetailsFragment);
        }
    }

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.home_activity_log_detail;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    private void changeFragment(Fragment fragment) {
        FragmentUtil.add(this, getContentId(), fragment, null, false);
    }

    private int getContentId() {
        return R.id.home_content_view;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
            finish();
            return;
        }
        super.onBackPressed();
    }
}
